/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJPanel.java
 *
 * Created on Jan 8, 2012, 10:44:35 PM
 */
package GUI;

import Bussiness.NhapHang;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Admin
 */
public class jpNhapHang extends javax.swing.JPanel {
    
    /** Creates new form NewJPanel */
     static String bot;
    private DefaultTableModel modelTable = new DefaultTableModel();
    public jpNhapHang() {
        initComponents();
        bot = "";
        this.display();
        this.teststatus();
    }

    public void add(){
        NhapHang b = new NhapHang();
        b.setMahang(txtMaHang.getText());
        b.setSoPhieu(txtSoPhieu.getText());
        b.setSoLuongXuat(Integer.parseInt(txtSLXuat.getText()));
        b.setNgayXuat(txtNgayXuat.getText());
        b.setMaKH(txtMaKH.getText());
        
        int t = 0;
        try{
            t = b.insertNhapHang(b);
        }catch(SQLException e){
            e.printStackTrace();
        }
        if(t==1)
            JOptionPane.showMessageDialog(this,"Add successful","Infofrm",JOptionPane.INFORMATION_MESSAGE);
        else
            JOptionPane.showMessageDialog(this,"Add erro!");
       this.reset();
    }
    
    public void search(){
        NhapHang b=new NhapHang();
        Vector v=new Vector();        
        try {
            if (cboTimKiem.getSelectedIndex()==0)
                v=b.getNhapHang_MaHang(txtTimKiem.getText());
            else
                 if (cboTimKiem.getSelectedIndex()==1)
                v=b.getNhapHang_SoPhieu(txtTimKiem.getText());
            else
                v=b.getNhapHang_NgayXuat(txtTimKiem.getText());    
            
        } catch (SQLException e) {
            e.printStackTrace();
        }  
          this.loadTable(v);
    }
     public void update()
    {             

        NhapHang c = new NhapHang();
        c.setMahang(txtMaHang.getText());
        c.setSoPhieu(txtSoPhieu.getText());
        c.setSoLuongXuat(Integer.parseInt(txtSLXuat.getText()));
        c.setNgayXuat(txtNgayXuat.getText());
        c.setMaKH(txtMaKH.getText());
        
        int t = 0;
        try{
            t = c.updateNhapHang(c);
        }catch(SQLException e){
            e.printStackTrace();
        }
        if(t==1)
            JOptionPane.showMessageDialog(this,"UPdate successful","Infofrm",JOptionPane.INFORMATION_MESSAGE);
        else
            JOptionPane.showMessageDialog(this,"Update erro!");
     
    }
     public void loadTable(Vector v)
    {
        modelTable.setNumRows(0);
        Vector v1 = new Vector();
        v1.addElement("Mã Hàng");
        v1.addElement("Số Phiêu");
        v1.addElement("Số Lượng Xuất");
        v1.addElement("Ngày Xuất");
        v1.addElement("Mã KH");
       
        modelTable.setColumnIdentifiers(v1);
        NhapHang b = new NhapHang();
        for (int i = 0; i < v.size(); i++) {
            b = (NhapHang)v.get(i);
            Vector row = new Vector();
            row.addElement(b.getMahang());
            row.addElement(b.getSoPhieu());
            row.addElement(b.getSoLuongXuat());
            row.addElement(b.getNgayXuat());
            row.addElement(b.getMaKH());
            modelTable.addRow(row);
        }
        tblNhapHang.setModel(modelTable);
        int r = tblNhapHang.getSelectedRow();
        if(r < 0){
            this.bot = "";
            btnSua.setEnabled(false);
            btnXoa.setEnabled(false);
            return;
        }
        else{
            bot = tblNhapHang.getValueAt(r, 0).toString();
        }
    }
   
      public void display()
    {
        NhapHang b1 = new NhapHang();
        Vector v1 = new Vector();
        try {
           v1=b1.getAllNhapHang(); 
        } catch (SQLException e) {
            e.printStackTrace();
        }
        this.loadTable(v1);
    }
      public void teststatus()
    {
        if(FrmLogin.status == "nhanvien"){
           btnSua.setEnabled(false);
           btnXoa.setEnabled(false);
        }
    }
       public void reset()
    {
        txtMaHang.setText("");
        txtSoPhieu.setText("");
        txtSLXuat.setText("");
        txtNgayXuat.setText("");
        txtMaKH.setText("");
        
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtMaHang = new javax.swing.JTextField();
        txtSoPhieu = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtSLXuat = new javax.swing.JTextField();
        txtNgayXuat = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        btnSua = new javax.swing.JButton();
        btnThem = new javax.swing.JButton();
        btnXoa = new javax.swing.JButton();
        btnLoad = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        txtMaKH = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblNhapHang = new javax.swing.JTable();
        cboTimKiem = new javax.swing.JComboBox();
        txtTimKiem = new javax.swing.JTextField();
        btnTimKiem = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();

        jLabel1.setText("Mã Hàng");

        jLabel2.setText("Số Phiếu");

        jLabel3.setText("Số Lượng Xuất");

        jLabel4.setText("Ngày Xuất");

        btnSua.setText("Sửa");
        btnSua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSuaActionPerformed(evt);
            }
        });

        btnThem.setText("Thêm");
        btnThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThemActionPerformed(evt);
            }
        });

        btnXoa.setText("Xóa");
        btnXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnXoaActionPerformed(evt);
            }
        });

        btnLoad.setText("Load");
        btnLoad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoadActionPerformed(evt);
            }
        });

        jLabel6.setText("Mã KH");

        tblNhapHang.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblNhapHang.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblNhapHangMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblNhapHang);

        cboTimKiem.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Mahang", "SoPhieu", "NgayXuat" }));
        cboTimKiem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboTimKiemActionPerformed(evt);
            }
        });

        btnTimKiem.setText("Tìm Kiếm");
        btnTimKiem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTimKiemActionPerformed(evt);
            }
        });

        jLabel7.setText("Tìm theo: ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 685, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(435, Short.MAX_VALUE)
                .addComponent(jLabel7)
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(btnTimKiem))
                    .addComponent(txtTimKiem, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cboTimKiem, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(43, 43, 43))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(177, 177, 177)
                        .addComponent(btnThem, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnSua, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(33, 33, 33)
                        .addComponent(btnXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(49, 49, 49)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(txtSoPhieu)
                            .addComponent(txtMaHang, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE))
                        .addGap(47, 47, 47)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addGap(26, 26, 26)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtSLXuat)
                            .addComponent(txtNgayXuat, javax.swing.GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE))))
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(txtMaKH, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(80, 80, 80))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnLoad, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(124, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(53, 53, 53)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(txtSLXuat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(txtNgayXuat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtMaKH, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(txtMaHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtSoPhieu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(43, 43, 43)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnThem)
                    .addComponent(btnSua)
                    .addComponent(btnXoa)
                    .addComponent(btnLoad))
                .addGap(28, 28, 28)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29)
                .addComponent(cboTimKiem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtTimKiem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnTimKiem)
                .addContainerGap(23, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void tblNhapHangMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblNhapHangMouseClicked
// TODO add your handling code here:
      int r = tblNhapHang.getSelectedRow();
    if(r < 0){
        bot = "";
        return;
    }else{
        btnSua.setEnabled(true);
        btnXoa.setEnabled(true);
        bot = tblNhapHang.getValueAt(r, 0).toString();
        
        if (r != -1){
            txtMaHang.setText(String.valueOf(tblNhapHang.getValueAt(r, 0)));
            txtSoPhieu.setText(String.valueOf(tblNhapHang.getValueAt(r, 1)));
            txtSLXuat.setText(String.valueOf(tblNhapHang.getValueAt(r, 2)));
            txtNgayXuat.setText(String.valueOf(tblNhapHang.getValueAt(r, 3)));
            txtMaKH.setText(String.valueOf(tblNhapHang.getValueAt(r, 4)));
           
        }
    }
}//GEN-LAST:event_tblNhapHangMouseClicked

private void btnSuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSuaActionPerformed
// TODO add your handling code here:
    this.update();
    this.display();
}//GEN-LAST:event_btnSuaActionPerformed

private void btnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThemActionPerformed
// TODO add your handling code here:
    this.add();
    this.display();
}//GEN-LAST:event_btnThemActionPerformed

private void btnLoadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoadActionPerformed
// TODO add your handling code here:
    this.display();
}//GEN-LAST:event_btnLoadActionPerformed

private void btnXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnXoaActionPerformed
// TODO add your handling code here:
    if(!bot.equals("")){
        int show=JOptionPane.showConfirmDialog(this,"Are You sure delete ?","Inform",JOptionPane.YES_NO_OPTION);
        int t=0,count=0;
        NhapHang a =new NhapHang();
        if(show==JOptionPane.YES_OPTION){
            int [] rArr=tblNhapHang.getSelectedRows();
                try 
                {
                    for(int i=0;i<rArr.length;i++)
                    {
                        t=a.DelNhapHang(tblNhapHang.getValueAt(rArr[i],0).toString());
                        if(t==1)
                            count++;
                    }
                    if(count>0)
                    {
                        JOptionPane.showMessageDialog(this,"Deleted "+count+" Row ","Successful.",JOptionPane.INFORMATION_MESSAGE);
                        this.display();
                    }
                    else
                    {
                      JOptionPane.showMessageDialog(this,"Error","Inform",JOptionPane.ERROR_MESSAGE);  
                    }
                } catch (SQLException x) {               
                   System.out.println(x);
                }
        }
    }
    this.reset();
}//GEN-LAST:event_btnXoaActionPerformed

private void btnTimKiemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTimKiemActionPerformed
// TODO add your handling code here:
    this.search();
}//GEN-LAST:event_btnTimKiemActionPerformed

private void cboTimKiemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboTimKiemActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_cboTimKiemActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnLoad;
    private javax.swing.JButton btnSua;
    private javax.swing.JButton btnThem;
    private javax.swing.JButton btnTimKiem;
    private javax.swing.JButton btnXoa;
    private javax.swing.JComboBox cboTimKiem;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblNhapHang;
    private javax.swing.JTextField txtMaHang;
    private javax.swing.JTextField txtMaKH;
    private javax.swing.JTextField txtNgayXuat;
    private javax.swing.JTextField txtSLXuat;
    private javax.swing.JTextField txtSoPhieu;
    private javax.swing.JTextField txtTimKiem;
    // End of variables declaration//GEN-END:variables
}
